package com.tfswx.ruleengine.compute.util;

import com.tfswx.ruleengine.compute.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Ricky
 * @date 2020/12/8
 */
@Slf4j
public class DateUtil {
    private DateUtil() {
    }

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static Date parse(String text) {
        return parse(text, DEFAULT_PATTERN);
    }

    public static Date parse(String text, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(text);
        } catch (ParseException e) {
            throw new AppException("日期转换失败：" + text);
        }
    }

    public static String format(Date date) {
        return format(date, DEFAULT_PATTERN);
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 获得当前日期时间
     * <p>
     * 如：2019-08-27 14:12:40
     *
     * @return
     */
    public static String getNowStr() {
        return format(new Date());
    }

    public static String getNowStr(String pattern) {
        return format(new Date(), pattern);
    }

    public static Date getFirstOfYear() {
        return getFirstOfYear(new Date());
    }

    public static Date getFirstOfYear(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.MONTH, Calendar.JANUARY);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        return instance.getTime();
    }

    public static Date getLastOfYear() {
        return getLastOfYear(new Date());
    }

    public static Date getLastOfYear(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.MONTH, Calendar.UNDECIMBER);
        instance.set(Calendar.DAY_OF_MONTH, 0);
        instance.set(Calendar.HOUR_OF_DAY, 23);
        instance.set(Calendar.MINUTE, 59);
        instance.set(Calendar.SECOND, 59);
        return instance.getTime();
    }

    public static Date getFirstOfMonth() {
        return getFirstOfMonth(new Date());
    }

    public static Date getFirstOfMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        return instance.getTime();
    }

    public static Date getLastOfMonth() {
        return getLastOfMonth(new Date());
    }

    public static Date getLastOfMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.MONTH, 1);
        instance.set(Calendar.DAY_OF_MONTH, 0);
        instance.set(Calendar.HOUR_OF_DAY, 23);
        instance.set(Calendar.MINUTE, 59);
        instance.set(Calendar.SECOND, 59);
        return instance.getTime();
    }

    public static Date getFirstOfDate() {
        return getFirstOfDate(new Date());
    }

    public static Date getFirstOfDate(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        return instance.getTime();
    }

    public static Date getLastOfDate() {
        return getLastOfDate(new Date());
    }

    public static Date getLastOfDate(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.HOUR_OF_DAY, 23);
        instance.set(Calendar.MINUTE, 59);
        instance.set(Calendar.SECOND, 59);
        return instance.getTime();
    }

    public static void main(String[] args) {
        String date = "20201107";
        Date from = DateUtil.parse(date, "yyyyMMdd");
        log.info(String.valueOf(from.getTime()));
        log.info(DateUtil.format(new Date(), "yyyy-MM-01"));
        log.info(DateUtil.format(DateUtil.getFirstOfMonth()));
        log.info(DateUtil.format(DateUtil.getLastOfMonth()));
        log.info(DateUtil.format(DateUtil.getFirstOfDate()));
        log.info(DateUtil.format(DateUtil.getLastOfDate()));
        log.info(DateUtil.format(DateUtil.getFirstOfYear()));
        log.info(DateUtil.format(DateUtil.getLastOfYear()));
        log.info(DateUtil.format(DateUtil.getLastOfMonth(DateUtil.parse("2020-02-01", "yyyy-MM-dd"))));
        log.info(DateUtil.format(DateUtil.getLastOfMonth(DateUtil.parse("2021-02-01", "yyyy-MM-dd"))));
    }
}
